﻿using HardcoreGame.Common.CustomSets;
using HardcoreGame.Common.Helpers;
using HardcoreGame.Common.Players;
using HardcoreGame.Content.Dusts;
using HardcoreGame.Content.Projectiles.Sets.Acid;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Items.Weapons.Sets.Acid
{
    public class PoisonCorrosion : ModItem
    {
        public static Lazy<Asset<Texture2D>> glowmask;
        public override void Unload()
        {
            glowmask = null;
        }
        public override void SetStaticDefaults()
        {
            CustomItemSets.AcidWeapon[Type] = true;
        }
        public override void SetDefaults()
        {
            Item.damage = 25;
            Item.DamageType = DamageClass.Ranged;
            Item.width = 48;
            Item.height = 26;
            Item.useTime = 35;
            Item.useAnimation = 35;
            Item.useStyle = ItemUseStyleID.Shoot;
            Item.noMelee = true;
            Item.knockBack = 2.25f;
            Item.value = Item.buyPrice(0, 1, 0, 0);
            Item.rare = ItemRarityID.Blue;
            Item.autoReuse = true;
            Item.shoot = ModContent.ProjectileType<CorrosionBullet>();
            Item.useAmmo = AmmoID.Bullet;
            Item.shootSpeed = 14f;
            Item.ChangePlayerDirectionOnShoot = false;
            Item.UseSound = SoundID.Item11;
            if (!Main.dedServ)
            {
                glowmask = new Lazy<Asset<Texture2D>>(() => ModContent.Request<Texture2D>(Texture + "_Glow", (AssetRequestMode)2));
            }
        }
        public override void HoldItem(Player player) => player.SyncMousePosition();
        public override bool CanConsumeAmmo(Item ammo, Player player) => true;
        public override void UseStyle(Player player, Rectangle heldItemFrame)
        {
            player.direction = Math.Sign((player.MouseWorld() - player.Center).X);
            float itemRotation = player.compositeFrontArm.rotation + MathHelper.PiOver2 * player.gravDir;
            Vector2 itemPosition = player.MountedCenter + itemRotation.ToRotationVector2() * 7f;
            Vector2 itemSize = new(48, 26);
            Vector2 itemOrigin = new(-15, 1);
            MyUtils.CleanHoldStyle(player, itemRotation, itemPosition, itemSize, itemOrigin);
            base.UseStyle(player, heldItemFrame);
        }
        public override float UseSpeedMultiplier(Player player)
        {
            return 1f;
        }
        public override void UseItemFrame(Player player)
        {
            player.direction = Math.Sign((player.MouseWorld() - player.Center).X);
            float animProgress = 1 - player.itemTime / (float)player.itemTimeMax;
            float rotation = (player.Center - player.MouseWorld()).ToRotation() * player.gravDir + MathHelper.PiOver2;
            if (animProgress < 0.4f)
            {
                rotation += -0.45f * (float)Math.Pow((0.4f - animProgress) / 0.4f, 2) * player.direction;
            }
            player.SetCompositeArmFront(true, Player.CompositeArmStretchAmount.Full, rotation);
        }
        public override void ModifyShootStats(Player player, ref Vector2 position, ref Vector2 velocity, ref int type, ref int damage, ref float knockback)
        {
            type = ModContent.ProjectileType<CorrosionBullet>();
            float animProgress = 1 - player.itemTime / (float)player.itemTimeMax;
            float rotation = (player.Center - player.MouseWorld()).ToRotation() * player.gravDir + MathHelper.PiOver2;

            if (animProgress < 0.4f)
            {
                rotation += -0.45f * (float)Math.Pow((0.4f - animProgress) / 0.4f, 2) * player.direction;
            }
            Vector2 offset = new Vector2(-(float)Math.Sin(rotation), (float)Math.Cos(rotation)) * 17f;
            position += offset;
            GenerateMuzzleParticles(player, rotation);
        }
        private static void GenerateMuzzleParticles(Player player, float rotation)
        {
            if (!Main.dedServ)
            {
                Vector2 weaponPosition = player.MountedCenter + new Vector2(50, 0) * player.direction;
                float cos = (float)Math.Cos(rotation);
                float sin = (float)Math.Sin(rotation);
                float offsetX = 24f;
                float offsetY = 10f;
                Vector2 muzzleOffset = new(
                    offsetX * player.direction * cos - offsetY * sin,
                    offsetX * player.direction * sin + offsetY * cos
                );
                Vector2 muzzlePosition = weaponPosition + muzzleOffset;
                for (int i = 0; i < 3; i++)
                {
                    float particleSpeed = Main.rand.NextFloat(0.5f, 1.5f);
                    float particleRotation = rotation + Main.rand.NextFloat(-0.5f, 0.5f);

                    Vector2 particleVelocity = new Vector2((float)Math.Sin(particleRotation), -(float)Math.Cos(particleRotation)) * particleSpeed;

                    int particleType = ModContent.DustType<AcidSmoke>();
                    if (Main.rand.NextBool(2)) particleType = ModContent.DustType<AcidSmoke>();

                    int dust = Dust.NewDust(muzzlePosition, 0, 0, particleType, 0f, 0f, 0, default, 1.2f);
                    Main.dust[dust].noGravity = true;
                    Main.dust[dust].velocity = particleVelocity;
                }
                for (int i = 0; i < 2; i++)
                {
                    float sparkSpeed = Main.rand.NextFloat(1f, 2f);
                    float sparkRotation = rotation + Main.rand.NextFloat(-0.3f, 0.3f);
                    Vector2 sparkVelocity = new Vector2((float)Math.Sin(sparkRotation), -(float)Math.Cos(sparkRotation)) * sparkSpeed;
                    int spark = Dust.NewDust(muzzlePosition, 0, 0, ModContent.DustType<AcidSmoke>(), 0f, 0f, 0, default, 1.5f);
                    Main.dust[spark].noGravity = true;
                    Main.dust[spark].velocity = sparkVelocity;
                }
            }
        }
        public override void PostDrawInWorld(SpriteBatch spriteBatch, Color lightColor, Color alphaColor, float rotation, float scale, int whoAmI)
        {
            MyUtils.BasicInWorldGlowmask(Item, spriteBatch, (Texture2D)glowmask.Value, Color.White, rotation, scale);
        }
    }
}
